package com.example.utils;

import com.example.common.response.CommonCode;
import com.example.common.response.ResponseResult;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

/**
 * @Author Lcyee
 * @Blog https://www.codeyee.com
 * @Date 2020/6/23
 * @Description
 */



public class JwtUtils {

    /**
     * 校验用户令牌是否有效
     * @param token 用户令牌
     * @return Boolean
     */
    public static boolean tokenVerify(String token) {
        String jwtSecret = "example";
        //生成jwt签名key
        Key jwtKey = new SecretKeySpec(jwtSecret.getBytes(), SignatureAlgorithm.HS512.getJcaName());   //签名私钥
        //令牌解密
        try {
            Claims claims = Jwts.parser()
                    .setSigningKey(jwtKey)
                    .parseClaimsJws(token)
                    .getBody();
        }catch (Exception ex){
            return false;
        }
        return true;
    }
}
